package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 韶关市供销合作联社
 * @author KK
 *
 */
public class GdsgcoopNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.gdsgcoop.com/zf11_news.asp\\?id=[0-9]*",
		"http://www.gdsgcoop.com/typenews.asp\\?id=[0-9]*",
		"http://www.gdsgcoop.com/index.php/[Aa-z0-9\\/]+"
	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
						
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("td[align=center] > strong").text();	
		if (StringUtils.isBlank(title)){
			title =doc.select("span.title").text();	
		}
		
		content=HtmlCleaner.getContentHtml(url,doc.select("td[height=34] > p"));			
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("p"));			
		}
		date=DateUtils.matchDate(doc.select("div:contains(发布：)").text());
		if (date==null){
			date=DateUtils.matchDate(doc.select("div.list").text());
		}

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setType(1);	
		return news;
	}
	
	public static void main(String[] args) {
		GdsgcoopNewsAnalyse analyse=new GdsgcoopNewsAnalyse();
		String url="http://www.gdsgcoop.com/index.php/Article/index/id/436";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
